<?php

class PollvotinghistoryController extends G4T_Controller
{
    public function indexAction()
    {
        $pollManager = new \Managers\PollVotingHistory();
        $longIP = ip2long($_SERVER['REMOTE_ADDR']);

        $active_poll = $pollManager->getActivePollId();
        if(!$active_poll){
            print_r("No active polls");
            return;
        }


        $array = array();
        foreach($active_poll as $apoll){
            $p = array_sum($apoll);

            if(!isset($_COOKIE["poll_id_".$p])){
                $data = $apoll['id'];
                $array[] = $data;
            }
        }
        $array2 = array();
        foreach($array as $apoll2){

            $checkIP = $pollManager->checkIP($longIP, $apoll2);
            //            die('Ok');
            if(!$checkIP){
                $data = $apoll2;
                $array2[] = $data;
            }
        }

        if( empty( $array2 ) ){

            $array2 = $active_poll;

            $random_key = array_rand($array2, 1);
            $randome_poll_id = $array2[$random_key] ;

            $poll_id = array_sum($randome_poll_id);

            $poll = $pollManager->getQuestion($poll_id);
            $poll_name = $poll['name'];
            $answers = $pollManager->getAnswersByPoolId($poll_id);

            $sum_count = 0;
            foreach($answers as $count){
                $sum_count = $sum_count*1 + ($count['votesCount']*1);
            }

            $this->view->sum_count = $sum_count;
            $this->view->poll_name = $poll_name;
            $this->view->answers = $answers;
            return;
        }
        $random_key = array_rand($array2, 1);
        $randome_poll_id = $array2[$random_key] ;

        $poll_id = $randome_poll_id;
        $poll = $pollManager->getQuestion($poll_id);
        $answers = $pollManager->getAnswersByPoolId($poll_id);
        $poll_name = $poll['name'];



        $this->view->answers = $answers;
        $this->view->poll_name = $poll_name;

    }
    public function ajaxindexAction()
    {
        $pollManager = new \Managers\PollVotingHistory();

        $answer_id = $this->_getParam('id');
        $pollManager->increase($answer_id);
        $answer = $pollManager->getPoolIdByAnswersId($answer_id);
        $poll_id = $answer['question']['id'];
        $poll_name = $answer['question']['name'];

        $answers = $pollManager->getAnswersByPoolId($poll_id);

        $poll_id_str = strval($poll_id);
        $longIP = ip2long($_SERVER['REMOTE_ADDR']);

        $golos = $pollManager->setVotingHistory($poll_id,$answer_id,$longIP);

        setcookie("poll_id_".$poll_id, $poll_id_str, time()+3600);


        $sum_count = 0;
        foreach($answers as $count){
            $sum_count = $sum_count*1 + ($count['votesCount']*1);
        }
        $this->view->sum_count = $sum_count;
        $this->view->answers = $answers;
        $this->view->poll_name = $poll_name;
    }

    public function statisticAction()
    {
        $this->_helper->layout()->setLayout('poll-layout');
        $pollManager = new \Managers\PollVotingHistory();


        $results = $pollManager->getPullResults();

        $this->view->results = $results;

    }

}
